package com.borrow.jiemihua.mainUI.activity;

import android.content.Intent;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.borrow.jiemihua.R;
import com.borrow.jiemihua.base.BaseActivity;
import com.borrow.jiemihua.base.BaseRVAdapter;
import com.borrow.jiemihua.base.BaseViewHolder;
import com.borrow.jiemihua.base.URLConstant;
import com.borrow.jiemihua.bean.IntenlligentBean;
import com.borrow.jiemihua.utils.xutils3.MyCallBack;
import com.borrow.jiemihua.utils.xutils3.XUtil;
import com.borrow.jiemihua.view.pull.PullLoadMoreRecyclerView;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

@ContentView(R.layout.activity_intelligent_match)
public class IntelligenHotActivity extends BaseActivity {
    @ViewInject(R.id.recyclerView)
    private PullLoadMoreRecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        initData();
        initView();
        setRecyclerMath();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        recyclerView.setLinearLayout();
    }

    @Event(value = {R.id.imgbtn_left}, type = View.OnClickListener.class)
    private void getEvent(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.imgbtn_left:
                finish();
                break;
            default:
                break;

        }
    }


    private List<IntenlligentBean.DataBean> dataBeanList = new ArrayList<>();
    private BaseRVAdapter rvAdapter;

    private void setRecyclerMath() {
        rvAdapter = new BaseRVAdapter(this, dataBeanList) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.main_recycler_item2;
            }

            @Override
            public void onBind(BaseViewHolder holder, final int position) {
                ImageView iv_main_image = holder.getImageView(R.id.iv_main_image);
                Glide.with(IntelligenHotActivity.this).load(dataBeanList.get(position).getLogo()).into(iv_main_image);
                String maxM = String.valueOf(dataBeanList.get(position).getMaxAmount());
                String minM = String.valueOf(dataBeanList.get(position).getMinAmount());
                String manED = null;
                String minED = null;
                DecimalFormat df = new DecimalFormat("######0.0");

                if (!maxM.equals("")) {
                    if (maxM.length() >= 5) {
                        String str = String.valueOf(df.format(Double.parseDouble(maxM) / 10000));
                        int ii = Integer.parseInt(str.substring(str.length() - 1, str.length()));
                        if (ii > 0) {
                            manED = str + "万";
                        } else {
                            manED = String.valueOf(Integer.parseInt(maxM) / 10000) + "万";
                        }
//                        manED = String.valueOf(Double.parseDouble(maxM) / 10000) + "万";
                    } else if (maxM.length() == 4) {
                        String str = String.valueOf(df.format(Double.parseDouble(maxM) / 1000));
                        int ii = Integer.parseInt(str.substring(str.length() - 1, str.length()));
                        if (ii > 0) {
                            manED = str + "千";
                        } else {
                            manED = String.valueOf(Integer.parseInt(maxM) / 1000) + "千";
                        }
//                        manED = String.valueOf(Double.parseDouble(maxM) / 1000) + "千";
                    } else if (maxM.length() < 4) {
                        manED = maxM;
                    }
                }
                if (!minM.equals("")) {
                    if (minM.length() >= 5) {
                        String str = String.valueOf(df.format(Double.parseDouble(minM) / 10000));
                        int ii = Integer.parseInt(str.substring(str.length() - 1, str.length()));
                        if (ii > 0) {
                            minED = str + "万";
                        } else {
                            minED = String.valueOf(Integer.parseInt(minM) / 10000) + "万";
                        }
//                        minED = String.valueOf(Double.parseDouble(minM) / 10000) + "万";
                    } else if (minM.length() == 4) {
                        String str = String.valueOf(df.format(Double.parseDouble(minM) / 1000));
                        int ii = Integer.parseInt(str.substring(str.length() - 1, str.length()));
                        if (ii > 0) {
                            minED = str + "千";
                        } else {
                            minED = String.valueOf(Integer.parseInt(minM) / 1000) + "千";
                        }
//                        minED = String.valueOf(Double.parseDouble(minM) / 1000) + "千";
                    } else if (minM.length() < 4) {
                        minED = minM;
                    }
                }
                holder.getTextView(R.id.tv_name).setText(dataBeanList.get(position).getName());
                holder.getTextView(R.id.tv_kded).setText(minM + "~" + maxM + "元");
                holder.getTextView(R.id.tv_lv).setText("参考利润 " + dataBeanList.get(position).getRateStr());
                holder.getTextView(R.id.tv_peonum).setText(String.valueOf(dataBeanList.get(position).getApplyForCount()));
                if (dataBeanList.get(position).getLendType() == 1) {
                    holder.getTextView(R.id.tv_dklx).setText("现金贷");
                } else if (dataBeanList.get(position).getLendType() == 2) {
                    holder.getTextView(R.id.tv_dklx).setText("信用贷");
                } else if (dataBeanList.get(position).getLendType() == 3) {
                    holder.getTextView(R.id.tv_dklx).setText("大额贷");
                }
                double time = dataBeanList.get(position).getQuickestTime() / 60;
                holder.getTextView(R.id.tv_fktime).setText(dataBeanList.get(position).getQuickestTimeStr() + "放款");
                holder.getView(R.id.ll_item_all).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(new Intent(IntelligenHotActivity.this, PlatformDetailsActivity.class)
                                .putExtra("terraceId", dataBeanList.get(position).getTerraceId() + ""));
                    }
                });
            }

        };
        recyclerView.setAdapter(rvAdapter);
        upMatchData(1);
        recyclerView.setOnPullLoadMoreListener(new PullLoadMoreRecyclerView.PullLoadMoreListener() {

            @Override
            public void onRefresh() {
                upMatchData(1);
            }

            @Override
            public void onLoadMore() {
                upMatchData(2);
            }
        });
    }

    private int page;


    private void upMatchData(final int i) {
        Map<String, String> map = new HashMap<>();
        if (i == 1) {
            page = 1;
        } else {
            page++;
        }
        map.put("isHot", "2");
        map.put("page", page + "");
        map.put("pageSize", "12");
        Set keys = map.keySet();
        if (keys != null) {
            Iterator iterator = keys.iterator();
            while (iterator.hasNext()) {
                Object key = iterator.next();
                Object value = map.get(key);
                Log.e("--智能匹配参数热点--" + key, "" + value + "\n");
            }
        }
        XUtil.Get(URLConstant.SHOUYEREMEN, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
//                Log.i("==智能匹配列表", result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("isSuccess");
                    if (res.equals("1")) {
                        Gson gson = new Gson();
                        IntenlligentBean intenlligentBean = gson.fromJson(result, IntenlligentBean.class);
                        List<IntenlligentBean.DataBean> dataBean = intenlligentBean.getData();
                        if (i == 1) {
                            dataBeanList.clear();
                        }
                        rvAdapter.addDataLs(dataBean);
                        rvAdapter.notifyDataSetChanged();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
                recyclerView.setPullLoadMoreCompleted();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                ex.printStackTrace();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        upMatchData(1);
    }


}
